package com.wangtongxiao.controller;

import com.wangtongxiao.beans.Type;
import com.wangtongxiao.dto.ResuDTO;
import com.wangtongxiao.exception.CRUDException;
import com.wangtongxiao.services.TypeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/DictionaryType")
public class TypeController {
    @Autowired
    TypeServices typeServices;
        @RequestMapping("/getAll.action")
   public ArrayList<Type> getAll(){
            ArrayList<Type> all = typeServices.getAll();
            return all;
   }
   @RequestMapping("/add.action")
   public ResuDTO add(Type type) throws CRUDException {
            ResuDTO resuDTO = new ResuDTO();
            resuDTO.setResult(ResuDTO.SUCCESS);
       boolean add = typeServices.add(type);
       if (!add){
           throw  new CRUDException("数据字典添加失败");
       }
       return resuDTO;
   }
   @RequestMapping("/editBefore.action")
   public Type editBefore(String id){
       return typeServices.get(id);
   }
   @RequestMapping("/edit.action")
   public ResuDTO edit(Type type ) throws CRUDException {
            ResuDTO resuDTO = new ResuDTO();
            resuDTO.setResult(ResuDTO.SUCCESS);
       boolean edit = typeServices.edit(type);
       if (!edit){
           throw new CRUDException("数据字典修改失败");
       }

       return resuDTO;
   }
   @RequestMapping("del.action")
   public ResuDTO del(@RequestBody String[] ids) throws CRUDException {

       System.out.println(ids+"是我");
            ResuDTO resuDTO = new ResuDTO();
            resuDTO.setResult(ResuDTO.SUCCESS);
       boolean del = typeServices.del(ids);
       if (!del){
           throw new CRUDException("删除失败");
       }


       return resuDTO;

   }


}
